/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.front.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.front.domain.AgvMissionFront;
import me.zhengjie.front.domain.vo.AgvMissionQueryCriteria;
import me.zhengjie.front.mapper.AgvMissionFrontMapper;
import me.zhengjie.front.service.AgvMissionService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author XX
* @date 2024-05-21
**/
@Service
@RequiredArgsConstructor
public class AgvMissionServiceImpl extends ServiceImpl<AgvMissionFrontMapper, AgvMissionFront> implements AgvMissionService {

    private final AgvMissionFrontMapper agvMissionFrontMapper;

    @Override
    public PageResult<AgvMissionFront> queryAll(AgvMissionQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(agvMissionFrontMapper.findAll(criteria, page));
    }

    @Override
    public List<AgvMissionFront> queryAll(AgvMissionQueryCriteria criteria){
        return agvMissionFrontMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AgvMissionFront resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgvMissionFront resources) {
        AgvMissionFront agvMissionFront = getById(resources.getId());
        agvMissionFront.copy(resources);
        saveOrUpdate(agvMissionFront);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<AgvMissionFront> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AgvMissionFront agvMissionFront : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("agv任务号", agvMissionFront.getTaskId());
            map.put("任务类型", agvMissionFront.getMissionType());
            map.put("任务状态", agvMissionFront.getTaskStatus());
            map.put("优先级", agvMissionFront.getPriority());
            map.put("途经点", agvMissionFront.getTargetCollection());
            map.put("动作参数总数", agvMissionFront.getTotalActionNum());
            map.put("已完成动作数量", agvMissionFront.getCompleteActionNum());
            map.put("执行agv", agvMissionFront.getAgvId());
            map.put("搬运栈板号", agvMissionFront.getPalletCode());
            map.put("WCS任务号", agvMissionFront.getWcsMissionId());
            map.put("wcs任务类型", agvMissionFront.getWcsMissionType());
            map.put("所属wcs任务状态", agvMissionFront.getWcsMissionStatus());
            map.put("创建时间", agvMissionFront.getCreateTime());
            map.put("开始时间", agvMissionFront.getStartTime());
            map.put("结束时间", agvMissionFront.getFinishTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}